﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*For observable collection */
using System.Collections.ObjectModel;


/*Using GasData namespace*/
using GasData;

namespace WPF_DataLogger
{
    public partial class GasTable
    {
        public GasTable()
        {
            _gas_data_table = new Dictionary<string, GasInfo>();
        }

        public Dictionary<string, GasInfo> The_table
        {
            get
            {
                return _gas_data_table;
            }
        }

        public void Clear()
        {
            _gas_data_table.Clear();
        }

        public void AddNewGasItem(KeyValuePair<String, GasInfo> new_gas_info)
        {
            _gas_data_table.Add(new_gas_info.Key, new_gas_info.Value);
        }

        public int Size()
        {
            return _gas_data_table.Count;
        }

        public GasInfo GetGasInfo(string gas_name, ref bool success)
        {
            GasInfo gasInfo;

            if (!_gas_data_table.ContainsKey(gas_name))
            {
                success = false;
                return null;
            }

            gasInfo = new GasInfo(_gas_data_table[gas_name]);


            return gasInfo;
        }

        public LinkedList<string> GetGasNameList()
        {
            LinkedList<string> ret = new LinkedList<string>();

            if (_gas_data_table.Count > 0)
            {
                foreach (KeyValuePair<string, GasInfo> gasinfo in _gas_data_table)
                {
                    ret.AddLast(gasinfo.Key);
                }
            }

            return ret;
        }

        public void UpdateGasTableContent(ObservableCollection<GasInfo> gas_list)
        {
            foreach (GasInfo gas in gas_list)
            {
                GasInfo gas_item = new GasInfo();

                gas_item.Cur_val = gas.Cur_val;
                gas_item.Gas_name = gas.Gas_name;

                if (!_gas_data_table.ContainsKey(gas_item.Gas_name))
                {
                    gas_item.Average = gas_item.Cur_val;
                    gas_item.Max = gas_item.Cur_val;
                    gas_item.Min = gas_item.Cur_val;
                    gas_item.Sample_count = 1;
                    gas_item.Variance = 0.0;

                    _gas_data_table.Add(gas_item.Gas_name, new GasInfo(gas_item));
                }
                else
                {
                    _gas_data_table[gas_item.Gas_name].UpdateGasInfo(gas_item.Cur_val);
                }
            }
        }

        Dictionary<String, GasInfo> _gas_data_table;
    }
}
